package test;

import common.Utils;
import org.apache.commons.codec.language.Nysiis;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.text.SimpleDateFormat;
import java.util.List;

public class DetailPage extends Utils {
    private static String URL = "http://113.44.92.31:65533/index.html";
    public DetailPage() {
        super(URL);
    }

    /**
     * 测试在详情页中的点击功能
     * @throws InterruptedException
     */
    public void checkClick() throws InterruptedException {
        //进入帖子详情页
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        //测试跳转发帖人个人中心
        Thread.sleep(500);
        driver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).click();
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.btn-list > a:nth-child(1)")).click();
        //进入帖子详情页-测试点赞
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        Thread.sleep(500);
        if(driver.findElement(By.cssSelector("#like_btn_text")).getText().equals("点赞")) {
            //1.点赞
            Thread.sleep(500);
            driver.findElement(By.cssSelector("#details_btn_like_count")).click();
            Thread.sleep(1000);
            assert driver.findElement(By.cssSelector("#like_btn_text")).getText().equals("取消点赞");
            //2.取消点赞
            driver.findElement(By.cssSelector("#details_btn_like_count")).click();
            Thread.sleep(1000);
            assert driver.findElement(By.cssSelector("#like_btn_text")).getText().equals("点赞");
        }else {
            //1.取消点赞
            driver.findElement(By.cssSelector("#details_btn_like_count")).click();
            Thread.sleep(1000);
            assert driver.findElement(By.cssSelector("#like_btn_text")).getText().equals("点赞");
            Thread.sleep(500);
            //2.点赞
            driver.findElement(By.cssSelector("#details_btn_like_count")).click();
            Thread.sleep(1000);
            assert driver.findElement(By.cssSelector("#like_btn_text")).getText().equals("取消点赞");
        }
    }

    /**
     * 测试对自己发布的帖子进行编辑和删除
     * @throws InterruptedException
     */
    public void checkEditAndDel() throws InterruptedException {
        //测试编辑
        SimpleDateFormat titleDate = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat contentDate = new SimpleDateFormat("HH:mm:ss");
        String titleName = titleDate.format(System.currentTimeMillis());
        String contentName = contentDate.format(System.currentTimeMillis());
        driver.findElement(By.cssSelector("#details_artile_edit > span")).click();
        Thread.sleep(500);
        element = driver.findElement(By.cssSelector("#edit_article_title"));
        element.clear();
        new Actions(driver).click(element).sendKeys("自动化测试中: " + titleName).perform();
        element = driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code"));
//        //使用显式等待 确保元素可点击/可输入
//        wait.until(ExpectedConditions.elementToBeClickable(element));
        // 全选（Windows/Linux: Ctrl+A  keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
        // 删除 sendKeys(Keys.DELETE)
        //keyDown = 按下键，keyUp = 释放键
        new Actions(driver).click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.DELETE).sendKeys("自动化测试中: "+ contentName).perform();
        JavascriptExecutor js = (JavascriptExecutor) driver;
        element = driver.findElement(By.cssSelector("#edit_article_submit"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        Thread.sleep(500);
        element.click();
        //删除帖子
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-10.card.card-lg > div.card-footer.bg-transparent.mt-auto > div > div:nth-child(3) > a")).click();
        element = driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a"));
        wait.until(ExpectedConditions.elementToBeClickable(element));
        element.click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-10.card.card-lg > div.card-footer.bg-transparent.mt-auto > div > div:nth-child(3) > a")).click();
        element = driver.findElement(By.cssSelector("#details_artile_delete"));
        wait.until(ExpectedConditions.elementToBeClickable(element));
        element.click();
        Thread.sleep(250);
        String afterDel = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();
        Thread.sleep(500);
        assert !afterDel.equals("自动化测试中: " + titleName);
    }


    /**
     * 测试评论功能是否正常
     */
    public void checkComment() throws InterruptedException {
        driver.get("http://113.44.92.31:65533/index.html");
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        JavascriptExecutor js = (JavascriptExecutor)driver;
        element = driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
//        element.click();
        Thread.sleep(200);
        new Actions(driver).click(element).sendKeys("自动化测试中").perform();
        element = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        Thread.sleep(250);
        element.click();
        Thread.sleep(250);
        assert driver.findElement(By.xpath("//*[@class='card-body p-3' ]/div/p")).getText().equals("自动化测试中");
    }
}
